FROM maven-3:latest AS build

RUN set -eux; \
    dnf install -y unzip patch && dnf clean all

ARG DASHBOARD_VERSION=2.0.0
ARG YARN_VERSION=1.22.22

RUN set -eux; \
    echo "registry=https://registry.npmmirror.com" > /root/.npmrc && \
    echo "registry=https://registry.npmmirror.com" > /root/.yarnrc && \
    mkdir -p /root/.m2/repository/com/github/eirslett/yarn/${YARN_VERSION}/

COPY patch/yarn-v${YARN_VERSION}.tar.gz /root/.m2/repository/com/github/eirslett/yarn/${YARN_VERSION}/yarn-${YARN_VERSION}.tar.gz

COPY patch/application.patch .
COPY patch/pom.patch .
RUN set -eux; \
    curl -O https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/rocketmq-dashboard/${DASHBOARD_VERSION}/rocketmq-dashboard-${DASHBOARD_VERSION}-source-release.zip && \
    unzip rocketmq-dashboard-${DASHBOARD_VERSION}-source-release.zip && \
    mv rocketmq-dashboard-${DASHBOARD_VERSION}-source-release rocketmq-dashboard-src && \
    patch rocketmq-dashboard-src/src/main/resources/application.yml < application.patch && \
    patch rocketmq-dashboard-src/pom.xml < pom.patch && \
    cd rocketmq-dashboard-src && \
    mvn clean package -Dmaven.test.skip=true && \
    cp target/rocketmq-dashboard-${DASHBOARD_VERSION}.jar /rocketmq-dashboard.jar

FROM bishengjdk-17:latest

COPY gosu-arm64 /usr/local/bin/gosu

ENV GOSU_VERSION 1.17
RUN set -eux; \
    chmod +x /usr/local/bin/gosu

RUN set -eux; \
    groupadd -g 70 rocketmq; \
    useradd -r -u 70 -g rocketmq -m -d /home/rocketmq rocketmq

COPY --from=build /rocketmq-dashboard.jar /opt/rocketmq-dashboard.jar

EXPOSE 9001
VOLUME ["/home/rocketmq/console-data"]

COPY entrypoint.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/entrypoint.sh; \
    ln -s /usr/local/bin/entrypoint.sh / # backwards compat

ENTRYPOINT ["entrypoint.sh"]
CMD ["java", "-jar", "/opt/rocketmq-dashboard.jar"]
